package com.luzj.controller;

import com.luzj.entity.User;
import com.luzj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/6.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/id")
    public User view( Long id) {
        User user = new User();
        id=30l;
        user.setId(id);
        user.setName("zhangXJ");
        return user;
    }

    @RequestMapping("/username/{username}")
    public String userProfile(@PathVariable("username") String username) {
        return String.format("user %s", username);
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginGet() {
        return "Login Page";
    }

    @Autowired
    private UserService userService;

    /**
     * 查询用户信息
     * @return 用户信息 Json格式
     */
    @RequestMapping("/findUserInfo")
    public List<User> getUserInfo(){
       List<User> users= userService.getUserInfo();
        if(users!=null){
            System.out.print("UserName:"+users.get(0).getName());
            return users;
        }
        return null;
    }

    /**
     * 删除编号233 的用户
     * @return 用户列表 Json
     */
    @RequestMapping("/deleteUserInfo")
    public List<User> delUser(){
        List<User> users = userService.deleteUserInfo();
        if(users!=null){
            System.out.print("UserName:"+users.get(0).getName());
            return users;
        }
        return null;
    }

    /**
     * 插入数据 id=233 name='minge'
     * @return 用户列表 Json格式
     */
    @RequestMapping("/insertUserInfo")
    public List<User> insertUser(){
        List<User> users = userService.insertUserInfo();
        if(users!=null){
            System.out.print("UserName:"+users.get(0).getName());
            return users;
        }
        return null;
    }

    /**
     * 修改 id=244 的name
     * @return 用户列表 Json
     */
    @RequestMapping("/updateUserInfo")
    public List<User> updateUserInfo(){
        List<User> users = userService.updateUserInfo();
        if(users!=null){
            System.out.print("UserName:"+users.get(0).getName());
            return users;
        }
        return null;
    }

}